<style lang="scss">
.ivu-modal-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  .ivu-modal{
    top: 0;
  }
}
</style>
<template>
  <div class="content">
    <Breadcrumb>
      <BreadcrumbItem to="/index">首页</BreadcrumbItem>
      <BreadcrumbItem>图片类型管理</BreadcrumbItem>
    </Breadcrumb>
    <Form inline :label-width="70" label-position="left" :model="search">
      <Button type="success" style="margin-right: 30px" @click="addModal = true">添加</Button>
      <FormItem label="类型名">
        <Input type="text" v-model="search.name" placeholder="请输入图片类型名检索"/>
      </FormItem>
      <FormItem label="类型描述">
        <Input type="text" v-model="search.imageTypeDesc" placeholder="请输入图片类型描述检索"/>
      </FormItem>
      <Button type="primary" style="margin-left: 30px" @click="searchClick">搜索</Button>
    </Form>
    <Table stripe row-key="id" :columns="columns" :data="data"></Table>
    <Page :current="current" :total="total" :page-size="pageSize" show-total show-sizer show-elevator :styles="{marginTop: '20px'}" @on-change="pageChange" @on-page-size-change="pageSizeChange" />
    <Modal
      v-model="modalShow"
      :title="addModal ? '添加图片类型' : editModal ? '修改图片类型' : '' + '灵异图片类型'"
      cancel-text="取消"
      class-name="center-modal"
      @on-visible-change="modalChange">
      <Form :model="form" :label-width="80" ref="modalForm" :rules="rules">
        <FormItem label="类型名" prop="name">
          <Input v-model="form.name" placeholder="请输入图片类型名"/>
        </FormItem>
        <FormItem label="类型描述" prop="imageTypeDesc">
          <Input v-model="form.imageTypeDesc" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" placeholder="请输入图片类型描述"/>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" size="large" @click="modalShow = false">取消</Button>
        <Button type="primary" size="large" @click="submit">{{ addModal ? '添加' : editModal ? '修改' : '' + '灵异' }}</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { delImage } from '@/api/image'
import { addImageType, imageTypeList, repeatImageType, updateImageType } from '@/api/imageType'

export default {
  name: 'ImageType',
  data: function () {
    return {
      columns: [
        {
          type: 'index'
        },
        {
          title: '图片类型名',
          key: 'name'
        },
        {
          title: '图片类型描述',
          tooltip: true,
          key: 'imageTypeDesc'
        },
        {
          title: '创建时间',
          key: 'createTime'
        },
        {
          title: '更新时间',
          key: 'updateTime'
        },
        {
          title: '操作',
          key: 'action',
          width: '260px',
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'warning',
                  size: 'small'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.form = { ...params.row }
                    this.editModal = true
                  }
                }
              }, '修改'),
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.$Modal.confirm({
                      title: '删除提示',
                      content: '是否确认删除?',
                      okText: '确定',
                      cancelText: '取消',
                      onOk: () => {
                        delImage(params.row.id).then(res => {
                          if (res.code === 200) {
                            this.getData()
                            this.$Message.success(res.msg)
                          }
                        })
                      }
                    })
                  }
                }
              }, '删除')
            ])
          }
        }
      ],
      data: [],
      addModal: false,
      editModal: false,
      form: {},
      rules: {
        name: [
          {
            required: true,
            type: 'string',
            message: '标签名不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '标签名长度在1-50位之间',
            trigger: 'blur'
          },
          {
            validator: (rule, val, cb) => {
              let params = `name=${val}`
              if (this.editModal) params += `&id=${this.form.id}`
              repeatImageType(params).then(res => {
                if (res.code === 200) {
                  if (res.data.flag) {
                    return cb(new Error('标签名已存在！'))
                  }
                  cb()
                }
              })
            },
            trigger: 'blur'
          }
        ],
        imageTypeDesc: [
          {
            required: true,
            type: 'string',
            message: '标签描述不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 300,
            message: '标签描述长度在300位之内',
            trigger: 'blur'
          }
        ]
      },
      total: 0,
      current: 1,
      pageSize: 10,
      search: {}
    }
  },
  mounted () {
    this.getData()
  },
  computed: {
    modalShow: {
      get () {
        return this.addModal || this.editModal
      },
      set (val) {
        this.addModal = this.editModal = false
      }
    }
  },
  methods: {
    getData () {
      imageTypeList(this.search, this.current, this.pageSize).then(res => {
        this.current = res.data.page.current
        this.total = res.data.page.total
        this.data = res.data.page.records
      })
    },
    modalChange (v) {
      if (!v) {
        this.$refs.modalForm.resetFields()
        this.form = {}
      }
    },
    submit () {
      this.$refs.modalForm.validate((v) => {
        if (v) {
          if (this.addModal) {
            addImageType(this.form).then(res => {
              if (res.code === 200) {
                this.getData()
                this.$Message.success(res.msg)
                this.addModal = false
              }
            })
          }
          if (this.editModal) {
            this.form.updateTime = null
            updateImageType(this.form).then(res => {
              if (res.code === 200) {
                this.getData()
                this.$Message.success(res.msg)
                this.editModal = false
              }
            })
          }
        }
      })
    },
    pageChange (index) {
      this.current = index
      this.getData()
    },
    pageSizeChange (size) {
      this.current = 1
      this.pageSize = size
      this.getData()
    },
    searchClick () {
      this.current = 1
      this.getData()
    }
  }
}
</script>

<style scoped lang="scss">
form {
  margin-top: 20px;
}
</style>
